<template>
  <!-- 一条文章单元格 -->
  <van-cell  >
    <!-- 标题区域的插槽 -->
    <template #title>
      <div class="title-box">
        <!-- 标题 -->
        <span>{{ obj.title }}</span>
        <!-- 单图 -->
        <img
          class="thumb"
          v-lazy="obj.cover.images[0]"
          v-if="obj.cover.type === 1"
        />
      </div>
      <!-- 三张图片 -->
      <div class="thumb-box" v-if="obj.cover.type > 1">
        <img
          class="thumb"
          v-for="(imgUrl, index) in obj.cover.images"
          :key="index"
          v-lazy="imgUrl"
        />
      </div>
</template>
    <!-- label 区域的插槽 -->
    <template #label>
      <div class="label-box">
        <div>
          <span>{{obj.aut_name}}</span>
          <span>{{obj.comm_count}}评论</span>
          <span>{{obj.pubdate}}</span>
        </div>
        <!-- 反馈按钮 -->
              <van-icon name="cross" @click.stop="show = true" v-if="showX" />
                  <van-action-sheet
                  v-model="show"
                  :cancel-text="cancelText"
                  :actions="actions"
                  @select="onSelect"
                  get-container="body"
                  @cancel="onCancel"
                  />
      </div>
    </template>
  </van-cell>
</template>

<script>
import { disLikwArcitleApi, articleReportsAPI } from '@/api'
import { firstActions, secondActions } from '@/api/reports.js'
import { Toast } from 'vant'
export default {
  props: {
    obj: {
      type: Object,
      default: () => {}
    },
    showX: {
      // 接受一个布尔值，来确定 X号是否显示，默认显示
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      firstActions,
      secondActions,
      show: false,
      actions: firstActions,
      cancelText: '取消'
    }
  },
  methods: {
    async onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      if (item.name === this.firstActions[1].name) {
        this.actions = this.secondActions
        this.cancelText = '返回'
      } else if (item.name === this.firstActions[0].name) {
        try {
          await disLikwArcitleApi({ target: this.obj.art_id })
          Toast('反馈成功!\n以为您减少此类文章推送')
          this.show = false
        } catch (error) {
          Toast('反馈失败，请稍后重试！')
          this.show = false
        }
      } else {
        try {
          await articleReportsAPI({
            type: item.value,
            target: this.obj.art_id,
            remark: '反正就是有问题的啦~'
          })
          Toast('成功反馈： ' + item.name)
          this.show = false
        } catch (error) {
          Toast('反馈失败，请稍后重试！')
          this.show = false
        }
      }
    },
    onCancel () {
      if (this.cancelText === '返回') {
        this.actions = this.firstActions
        this.cancelText = '取消'
        this.show = true
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  text-align: left;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span{
    margin: 0 3px;
    &:first-child{
        margin-left: 0;
    }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
